package base.thread.threadCommunication.xinHaoDengFa;

/**
 * @Author: my_code
 * @Create: 2020-12-09 15:10
 */
public class SignalLamp {

    boolean signal;

    public synchronized void RoadOnePass() {
        if (signal) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("RoadOne在通行");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.notifyAll();
        this.signal = !this.signal;
    }

    public synchronized void RoadTwoPass() {
        if (!signal) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("RoadTwo在通行");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.notifyAll();
        this.signal = !this.signal;
    }
}